#include <dos.h>
#include "mpx_supt.h"

#define INVALID_EVEN_FLAG_POINTER -101
#define INVALID_BAUD_RATE_DIVISOR -102
#define PORT_ALREADY_OPEN -103

#define OPEN 1
#define CLOSED 0

#define COM1_INT_ID 0x0c
#define COM1_BASE 0x3F8
#define COM1_INT_EN 0x3F9

#define COM1_BRD_LSB 0x3F8
#define COM1_BRD_MSB 0x3F9
#define COM1_INT_ID_REG COM1_BASE+2
#define COM1_LC COM1_BASE+3
#define COM1_MC COM1_BASE+4
#define COM1_LS COM1_BASE+5
#define COM1_MS COM1_BASE+6
#define PIC_MASK 0x21
#define PIC_CMD 0x20
#define EOI 0x20

#define INVALID_BUFFER_ADDRESS -302
#define INVALID_COUNT_ADDRESS_OR_VALUE -303
#define PORT_BUSY -304


#define PORT_NOT_OPEN -201

#define IDLE 0
#define READING 1
#define WRITING 2

#define RING_BUFFER_SIZE 256

// Structs

typedef struct dcb {

	int state;
	int *event_flag;
	int status_code;
	char *in_buff;
	int *in_count;
	int in_done;

	char *out_buff;
	int *out_count;
	int out_done;

	char ring_buffer [RING_BUFFER_SIZE];
	int ring_buffer_in;
	int ring_buffer_out;
	int ring_buffer_count;

}DCB;

DCB serial;

void interrupt (*saved_interrupt) (void);

// Prototypes

int com_open(int *eflag_p, int baud_rate);
int com_close(void);
int com_read(char *buf_p, int *count_p);
int com_write(char * buf_p, int *count_p);


// interrupt handler L1

void interrupt l1_int();


// interrupt handlers L2

void input_handler();
void output_handler();